package com.gohome.gohomechat.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Recorder;
import com.gohome.gohomechat.ui.viewlistener.VoiceRVListener;

import java.util.List;

/**
 * Created by dllo on 16/3/5.
 */
public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerViewAdapter.ViewHolder> {

    private List<Recorder> dataList;
    private LayoutInflater inflater;
    private int mMinItemWith;// 设置对话框的最大宽度和最小宽度
    private int mMaxItemWith;
    private Recorder recorder;

    private Context context;
    private int[] drawables = {R.drawable.chat_camera_select, R.drawable.chat_image_select,
            R.drawable.chat_location_select, R.drawable.chat_video_call_select,
            R.drawable.chat_file_select, R.drawable.chat_voice_call_select,
            R.drawable.chat_video_select};
    private String[] titles = {"拍照", "图片", "位置", "视频", "文件", "语音通话", "视频通话"};
    public OnClickListener listener;
    private VoiceRVListener voiceItemListener;
    private VoiceRVListener voiceItemLongListener;


    public ChatRecyclerViewAdapter(Context context, List<Recorder> recorder) {
        inflater = LayoutInflater.from(context);

        this.context = context;
        this.dataList = dataList;
        // 获取系统宽度
        WindowManager wManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.7f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);
    }


    public void setVoiceItemListener(VoiceRVListener voiceItemListener) {
        this.voiceItemListener = voiceItemListener;
    }

    public void setVoiceItemLongListener(VoiceRVListener voiceItemLongListener) {
        this.voiceItemLongListener = voiceItemLongListener;
    }

    public void setOnClickTypeListener(OnClickListener listener) {
        this.listener = listener;
    }

    public ChatRecyclerViewAdapter(Context context) {
        this.context = context;
    }




    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_type_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.chat_type_title.setText(titles[position]);
        Drawable drawable = context.getResources().getDrawable(drawables[position]);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        holder.chat_type_title.setCompoundDrawables(null, drawable, null, null);

        holder.chat_type_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClikType(holder.getLayoutPosition());
            }
        });

        if (voiceItemListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    voiceItemListener.voiceOnClickLisener(pos);
                }
            });
        }
        if (voiceItemLongListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    voiceItemLongListener.voiceOnLongClickLisener(pos);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView chat_type_title;

        private TextView secounds; // 语音时间；
        private View length; // 长度

        public ViewHolder(View itemView) {
            super(itemView);
            chat_type_title = (TextView) itemView.findViewById(R.id.chat_type_title);
            secounds = (TextView) itemView.findViewById(R.id.recorder_time);
        }
    }

    public interface OnClickListener {
        void onClikType(int position);
    }

}
